package com.tvein.sanguo.model
{
	//flash
	//pureMVC
	import org.puremvc.as3.multicore.interfaces.IProxy;
	import org.puremvc.as3.multicore.utilities.fabrication.patterns.proxy.FabricationProxy;
	//SFS
	import com.smartfoxserver.v2.SmartFox;
	import com.smartfoxserver.v2.entities.Room;
	import com.smartfoxserver.v2.core.SFSEvent;
	import com.smartfoxserver.v2.entities.data.SFSObject;
	import com.smartfoxserver.v2.entities.data.ISFSArray;
	import com.smartfoxserver.v2.entities.data.ISFSObject;
	import com.smartfoxserver.v2.entities.variables.RoomVariable;
	import com.smartfoxserver.v2.requests.*;
	import com.smartfoxserver.v2.bitswarm.*;
	//custom
	import com.tvein.sanguo.common.CommonConstants;
//
	public class SFSProxy extends FabricationProxy implements IProxy
	{
		public static const NAME:String = "SFSProxy";
		
		public function SFSProxy()
		{
			super(NAME);
		}
		
		//Custom
		public var sfs:SmartFox= new SmartFox();
		
		override public function onRegister() : void
        {
			//sfs.debug = true;
			
			// Add SFS2X event listeners
			sfs.addEventListener(SFSEvent.CONNECTION, onConnection);
			sfs.addEventListener(SFSEvent.CONNECTION_LOST, onConnectionLost);
			sfs.addEventListener(SFSEvent.LOGIN, onLogin);
			sfs.addEventListener(SFSEvent.LOGOUT, onLogOut);
			sfs.addEventListener(SFSEvent.ROOM_JOIN, onRoomJoin);
			sfs.addEventListener(SFSEvent.ROOM_JOIN_ERROR, onRoomJoinError);
			sfs.addEventListener(SFSEvent.USER_ENTER_ROOM, onUserEnterRoom);
			sfs.addEventListener(SFSEvent.USER_EXIT_ROOM, onUserExitRoom);
			sfs.addEventListener(SFSEvent.ADMIN_MESSAGE, onSFSMessage);
			sfs.addEventListener(SFSEvent.PUBLIC_MESSAGE, onSFSMessage);
			sfs.addEventListener(SFSEvent.PRIVATE_MESSAGE, onSFSMessage);
			sfs.addEventListener(SFSEvent.EXTENSION_RESPONSE, onResponse);
		}
		
		public function send(cmd:String, params:Object = null, room:Object = null, useUDP:Boolean = false):void
		{
			var tmpParams:SFSObject = new SFSObject();
			
			tmpParams.putUtfString("params", JSON.stringify(params));
			
			sfs.send(new ExtensionRequest(cmd, tmpParams, Room(room), useUDP));
		}
		
		private function onResponse(evt:SFSEvent):void
		{
			var results:ISFSObject = ISFSObject(evt.params.params);
			
			trace(results.getUtfString("Script"));
			
			switch(evt.params.cmd)
			{
				case "resErrorScript":
					var error:Object = JSON.parse(results.getUtfString("Script"));
					sendNotification(CommonConstants.SYSTEM_CONSOLE, ["-gameStatus", [int(error.Main.type), error.Process[0].msg]]);
					break;
				case "resActionScript":
					routeNotification(CommonConstants.MODULE_INVOKE, ["-script", JSON.parse(results.getUtfString("Script"))], null, CommonConstants.MODULE_ACTION + "/*");
					break;
				case "resBackGroundScript":
					routeNotification(CommonConstants.MODULE_INVOKE, ["-script", JSON.parse(results.getUtfString("Script"))], null, CommonConstants.MODULE_BACKGROUND + "/*");
					break;
				case "resCharacterScript":
					routeNotification(CommonConstants.MODULE_INVOKE, ["-script", JSON.parse(results.getUtfString("Script"))], null, CommonConstants.MODULE_CHARACTER + "/*");
					break;
				case "resChatScript":
					routeNotification(CommonConstants.MODULE_INVOKE, ["-script", JSON.parse(results.getUtfString("Script"))], null, CommonConstants.MODULE_CHAT + "/*");
					break;
				case "resCrowdScript":
					routeNotification(CommonConstants.MODULE_INVOKE, ["-script", JSON.parse(results.getUtfString("Script"))], null, CommonConstants.MODULE_CROWD + "/*");
					break;
				case "resEventScript":
					routeNotification(CommonConstants.MODULE_INVOKE, ["-script", JSON.parse(results.getUtfString("Script"))], null, CommonConstants.MODULE_EVENT + "/*");
					break;
				case "resPromptScript":
					routeNotification(CommonConstants.MODULE_INVOKE, ["-script", JSON.parse(results.getUtfString("Script"))], null, CommonConstants.MODULE_PROMPT + "/*");
					break;
				case "resRoomScript":
					routeNotification(CommonConstants.MODULE_INVOKE, ["-script", JSON.parse(results.getUtfString("Script"))], null, CommonConstants.MODULE_ROOM + "/*");
					break;
				case "resToolScript":
					routeNotification(CommonConstants.MODULE_INVOKE, ["-script", JSON.parse(results.getUtfString("Script"))], null, CommonConstants.MODULE_TOOL + "/*");
					break;
				case "resZoneScript":
					routeNotification(CommonConstants.MODULE_INVOKE, ["-script", JSON.parse(results.getUtfString("Script"))], null, CommonConstants.MODULE_ZONE + "/*");
					break;
			}
		}
		
		private function onUserEnterRoom(evt:SFSEvent):void
		{
			sendNotification(CommonConstants.GAME_CONSOLE, ["-roleRoomPass", [evt.params.user, "in"]]);
		}
		
		private function onUserExitRoom(evt:SFSEvent):void
		{
			sendNotification(CommonConstants.GAME_CONSOLE, ["-roleRoomPass", [evt.params.user, "out"]]);
		}
		
		private function onSFSMessage(evt:SFSEvent):void
		{
			sendNotification(CommonConstants.GAME_CONSOLE, ["-gameMessage", [evt.params.sender, evt.params.message, evt.params.data.getInt("type")]]);
		}
		
		private function onConnection(evt:SFSEvent):void
		{
			if (evt.params.success)
			{
				var now:Date = new Date();
				
				var tmpName:String = "Guest:" + Math.random();
				
				var request:LoginRequest = new LoginRequest(tmpName, "", "Tvein");
				sfs.send(request);
			}
			else
			{
				sendNotification(CommonConstants.SYSTEM_CONSOLE, ["-gameStatus", [1, "游戏服务器连接失败，请稍候再试..."]]);
			}
		}
		
		private function onConnectionLost(evt:SFSEvent):void
		{
			sendNotification(CommonConstants.SYSTEM_CONSOLE, ["-gameStatus", [2, "游戏服务器已断开，请重新登录..."]]);
		}
		
		private function onLogin(evt:SFSEvent):void
		{
			switch(sfs.currentZone.toString())
			{
				case "Tvein":
					sendNotification(CommonConstants.SYSTEM_CONSOLE, ["-tveinLogin"]);
					break;
				case "SanGuo":
					break;
			}
		}
		
		private function onLogOut(evt:SFSEvent):void
		{

		}
		
		private function onRoomJoin(evt:SFSEvent):void
		{
			sendNotification(CommonConstants.GAME_CONSOLE, ["-roomChange", [evt.params.room.name, convertRoomVariables(evt.params.room.getVariables())]]);
		} 
		
		private function onRoomJoinError(evt:SFSEvent):void
		{
			sendNotification(CommonConstants.SYSTEM_CONSOLE, ["-gameStatus", [2, "进入房间错误，请重新登录..."]]);
		}
		
		public function convertRoomVariables(variables:Array): Array
		{
			var resultArray:Array = new Array();
			
			for (var i:int = 0; i < variables.length; i++)
			{
				resultArray[i] = {};
				
				resultArray[i].name = variables[i].name;
				resultArray[i].value = variables[i].getValue();
			}
			
			return resultArray;
		}
	}
}